package com.smart.dao;


import com.smart.domain.Lesson;
import com.smart.domain.Teacher;
import com.smart.util.JDBCUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@Repository
public class TeacherDao {
    private JdbcTemplate jdbcTemplate;

     String sql = " SELECT count(*) FROM teacher  " + " WHERE teacher_name =? and teacher_password=? ";
    @Autowired // 自动注入jdbcTemplate 的bean
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    //插入

    public int  addteacher(Teacher teacher) {
        String sql = "insert into teacher(teacher_name,teacher_department,teacher_username,teacher_password,email,phonenumber,user_type) values(?,?,?,?,?,?,?)";
        Object[] params =new Object[]{teacher.getName(),teacher.getDepartment(),teacher.getUserName(),teacher.getPassword(),teacher.getEmail(),teacher.getPhoneNumber(),teacher.getUser_tye()};
        return jdbcTemplate.update(sql,params);
    }

    //删除老师的信息
    public int deleteteacher(Teacher teacher){
        String sql ="delete from teacher where teacher_name =?";

        return jdbcTemplate.update(sql,teacher.getName());
    }
    //修改老师的信息
    public  int updateteacher(Teacher teacher)
    {
        String sql ="update teacher set teacher_department=? ,teacher_username=? ,teacher_password=? ,email=? ,phonenumber=? where teacher_name=?";
        return jdbcTemplate.update(sql,teacher.getDepartment(),teacher.getUserName(),teacher.getPassword(),teacher.getEmail(),teacher.getPhoneNumber(),teacher.getName());
    }

    public int getMatchCount(String name,String password) {

        return jdbcTemplate.queryForObject(sql, new Object[]{name,password}, Integer.class);
    }


    public List<Teacher> getbyteachernameandps(String name, String pw) {
        List<Teacher> list1 = null;
        String sql="select * from teacher where teacher_name=? and teacher_password=?";
        List<Teacher> list=JDBCUtil.dbDQLWithSQL(sql, Teacher.class,name,pw);
        for (Teacher user : list) {
            if(user.getName().equals(name)&&user.getPassword().equals(pw)) {
                String sql1="select * from teacher";
                list1=JDBCUtil.dbDQLWithSQL(sql1, Teacher.class);
            }
            else break;
        }
        return list1;

    }


    //查询单个老师的信息
    public Teacher queryteacher(String name){
        String sql ="select * from teacher where teacher_name=?";
        Teacher com=null;
        try {
            com =jdbcTemplate.queryForObject(sql,new TeacherRowMapper(),name);
        }catch (EmptyResultDataAccessException e){
            return null;
        }

       return com;

    }





   //查询全部老师信息
    public  void queryteacherlist(Teacher teacher){
        String sql ="select * from teacher";
        List<Teacher> tea = jdbcTemplate.query(sql,new TeacherRowMapper() );
        System.out.println(tea);
    }

}



class TeacherRowMapper implements RowMapper<Teacher>{
    @Override
    public Teacher mapRow(ResultSet rs, int i) throws SQLException {

        //1 从结果集里得到数据
        String name =rs.getString("teacher_name");
        String department =rs.getString("teacher_department");
        String userName =rs.getString("teacher_username");
        String password = rs.getString("teacher_password");
        String email = rs.getString("email");
        String phoneNumber = rs.getString("phonenumber");

        //2 把得到数据封装到数据里面去
        Teacher teacher =new Teacher();
        teacher.setName(name);
        teacher.setDepartment(department);
        teacher.setUserName(userName);
        teacher.setPassword(password);
        teacher.setEmail(email);
        teacher.setPhoneNumber(phoneNumber);
        return teacher;
    }
}